package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.BillOfParcels;
import com.lvy.hczn.front.system.domain.business.Warehouse;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.ProductSupplierGoods;
import com.lvy.hczn.transmit.domain.ProductSupplierGoodsBarcode;
import com.lvy.hczn.transmit.domain.ProductionProcessing;
import com.lvy.hczn.transmit.domain.dto.cteck.ProcessingTask;
import com.lvy.hczn.transmit.domain.dto.cteck.ProcessingTaskData;
import com.lvy.hczn.transmit.service.ProductSupplierGoodsBarcodeService;
import com.lvy.hczn.transmit.service.ProductSupplierGoodsService;
import com.lvy.hczn.transmit.service.TProcessingTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @projectName: wms-front
 * @package: com.lvy.hczn.transmit.service.impl
 * @className: TProcessingTaskServiceImpl
 * @author: yw
 * @date: 2025/7/4 14:38
 */
@Service
@Slf4j
public class TProcessingTaskServiceImpl implements TProcessingTaskService {

    @Autowired
    private ProductSupplierGoodsService productSupplierGoodsService;

    @Autowired
    private ProductSupplierGoodsBarcodeService productSupplierGoodsBarcodeService;

    @Autowired
    private ProducerUtil producerUtil;

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 从平台同步生产加工任务
     *
     * @param request
     */
    @Override
    public void syncProcessingTaskFromPlatform(HttpServletRequest request, ProcessingTask processingTask) throws Exception {
        log.error("syncProcessingTaskFromPlatform:{}", JSON.toJSONString(processingTask));
        if (processingTask == null) {
            throw new UtilException("同步生产加工任务失败，数据信息为空");
        }
        if (StrUtil.isEmpty(processingTask.getServiceCode())) {
            throw new UtilException("业务单号为空");
        }
        if (StrUtil.isEmpty(processingTask.getRepId())) {
            throw new UtilException("仓库id为空");
        }
        if (processingTask.getList() == null || processingTask.getList().isEmpty()) {
            throw new UtilException("生产加工任务列表数据为空");
        }
        Warehouse warehouse = warehouseService.getOne(Wrappers.<Warehouse>lambdaQuery().eq(Warehouse::getWarehouseCode, processingTask.getRepId()).eq(Warehouse::getStatus, Constants.WAREHOUSE_STATUS_ENABLE).last("limit 1"));
        if (warehouse == null) {
            throw new UtilException("编号为" + processingTask.getRepId() + "的仓库信息不存在");
        }

        ParamInfo<ProductionProcessing> paramInfo = new ParamInfo<>();
        List<ProductionProcessing> list = new ArrayList<>();
        for (ProcessingTaskData processingTaskData : processingTask.getList()) {
            ProductionProcessing productionProcessing = new ProductionProcessing();
            BeanUtil.copyProperties(processingTaskData, productionProcessing);
            productionProcessing.setDone(false);
            ProductSupplierGoods productSupplierGoods = productSupplierGoodsService.getOne(Wrappers.<ProductSupplierGoods>lambdaQuery().eq(ProductSupplierGoods::getProductSysCode, processingTaskData.getProductCode()));
            if (productSupplierGoods == null) {
                throw new UtilException("商品信息不存在");
            }

            ProductSupplierGoodsBarcode productSupplierGoodsBarcode = productSupplierGoodsBarcodeService.getOne(Wrappers.<ProductSupplierGoodsBarcode>lambdaQuery().eq(ProductSupplierGoodsBarcode::getProductSysCode, processingTaskData.getProductCode()));
            if (productSupplierGoodsBarcode == null) {
                throw new UtilException("商品规格信息不存在");
            }
            productionProcessing.setWarehouseCode(processingTask.getRepId());
            productionProcessing.setServiceCode(processingTask.getServiceCode());
            productionProcessing.setServiceType(processingTask.getServiceType());
            productionProcessing.setId(processingTaskData.getId());
            productionProcessing.setSpuId(productSupplierGoods.getId());
            productionProcessing.setSkuCode(productSupplierGoodsBarcode.getBarcodeSysCode());
            productionProcessing.setSpecCode(productSupplierGoodsBarcode.getBarcodeSysCode());
            productionProcessing.setWeightOfPerBox(processingTaskData.getUnitWeight());
            productionProcessing.setQuantity(processingTaskData.getNum());
            productionProcessing.setTotalWeight(productionProcessing.getWeightOfPerBox().multiply(new BigDecimal(productionProcessing.getQuantity())));
            productionProcessing.setMaterialProcessId("0");
            productionProcessing.setSpuGradeId("0");
            productionProcessing.setClientId("0");
            if (processingTaskData.getProcessingTime() == null) {
                productionProcessing.setProcessingTime(LocalDateTime.now().plusHours(1));
            }
            list.add(productionProcessing);
        }
        String topic = warehouseService.selectRegionAreaTopic(warehouse);
        String tag = processingTask.getRepId() + "_" + MqConstants.TOPIC_PRODUCTION_PROCESSING_TASK;
        paramInfo.setWarehouseCode(processingTask.getRepId());
        paramInfo.setList(list);
        producerUtil.sendAsyncMsg(topic, tag, JSON.toJSONString(paramInfo).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
    }
}
